package com.bocai.dahuatech.ui.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.widget.TextView;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.Bean.PointBean;
import com.bocai.dahuatech.Bean.PointShopBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.ui.adapter.PointsAdapterHolder;
import com.bocai.dahuatech.ui.pressent.PointListPressent;
import com.bocai.dahuatech.ui.view.LoadMoreRecyclerView;
import com.bocai.dahuatech.utils.AESUtils;
import com.bocai.dahuatech.utils.SharePrefencesUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * 积分商城
 */
public class PointsListActivity extends BaseActivity implements PointListPressent {

    @Bind(R.id.rv_points)
    LoadMoreRecyclerView rvPoint;
    @Bind(R.id.swipe_points)
    SwipeRefreshLayout swippoint;
    @Bind(R.id.tv_point)
    TextView tvPoint;
    private PointsAdapterHolder adapter;
    PointShopBean  bean;
    List<PointShopBean.ContentEntity> datas=new ArrayList<>();
    int page=1;
    PointBean pointBean;
    Double userPoint;//可用华积分
    @Override
    protected int contentView() {
        return R.layout.activity_points_list;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        initToolbar(R.string.pointshop);
        rvPoint.setLayoutManager(new LinearLayoutManager(this));
        initEvent();
        getUserPoint();

    }

    private void getUserPoint() {
        getTestApi().getPoint(SharePrefencesUtil.getCustomerCode(this))
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(pointObserver);
    }
    Observer<BaseBean> pointObserver=new Observer<BaseBean>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            hideLoading();
            showToast(R.string.neterror);
            Log.e("result","zhang");
        }

        @Override
        public void onNext(BaseBean baseBean) {
            if(baseBean.getReturnNo().equals("1")) {

                String result= AESUtils.decode(baseBean.getContent());
                result=start+result+end;
                Log.e("result", result);
                pointBean=new Gson().fromJson(result,PointBean.class);
//                userPoint=pointBean.getContent().get(0).getFAvailableCoin();
                tvPoint.setText(getResources().getText(R.string.canusepoint)+pointBean.getContent().get(0).getFAvailableCoin());
                setAdapter();
                getDatas();
            }
        }
    };
    private void setAdapter() {
        adapter = new PointsAdapterHolder(PointsListActivity.this,datas,userPoint,this);
        rvPoint.setAdapter(adapter);
    }
    @Override
    protected void initEvent() {
        super.initEvent();
        swippoint.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                datas.clear();
                getDatas();
            }
        });
        rvPoint.setLoadMoreListener(new LoadMoreRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getDatas();
            }
        });
    }

    private void getDatas() {
        showLoading();
        getTestApi().getPointShop(page, 20)
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    Observer<BaseBean> observer=new Observer<BaseBean>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            hideLoading();
            showToast(R.string.neterror);
            swippoint.setRefreshing(false);
            Log.e("error","error");
        }

        @Override
        public void onNext(BaseBean baseBean) {
            hideLoading();
            swippoint.setRefreshing(false);
            if(baseBean.getReturnNo().equals("1"))
            {
                String result= AESUtils.decode(baseBean.getContent());
                result=start+result+end;
                Log.e("result", result);
                bean=new Gson().fromJson(result,PointShopBean.class);
                datas.addAll(bean.getContent());
                adapter.notifyDataSetChanged();
                if(bean.getContent().size()<20)
                {
                    rvPoint.notifyMoreFinish(false);
                }else
                {
                    rvPoint.notifyMoreFinish(true);
                }
            }else
            {
                showToast(baseBean.getReturnInfo());
            }
        }
    };

    @Override
    public void getAddrList() {
        getTestApi().getAddrList(SharePrefencesUtil.getCustomerCode(this))
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(adapter.addrsObserver);
    }

    @Override
    public void commitPoint(int productId,double totalPoint, int totalNum, int addrId) {
        getTestApi().toExchange(productId,totalPoint,totalNum,addrId,SharePrefencesUtil.getFID(this),SharePrefencesUtil.getCustomerCode(this))
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(adapter.commitObserver);
    }

    @Override
    public void finishActivity() {
        finish();
    }
}
